<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardModelJASendEmail extends JModel
{
	/*
		Input: 
		@Username:User send mail
		@EmailName: template name send
		@arrParam: array parametters value
		OutPut: Success:return success
			Erro: return false
		
	*/
	var $jbconfig;
	
	function __construct( $default = array())
	{
		parent::__construct( $default );
		$this->config = new stdClass();
		$db = &JFactory::getDBO();
		$sql="Select * From #__ja_config ";
		
		$db->setQuery($sql);
		$rows=$db->loadObjectList();
		for($i=0,$n=count($rows);$i<$n;$i++)
		{
			$row=$rows[$i];
			$key=$row->ckey;
			$value=$row->cvalue;
			$this->config->$key=$value;
		}		
		echo 'contruct';
	}
	
    function sendmail($userid,$emailname,$arrParam)
    {
    	$db = &JFactory::getDBO();
    	//
    	$from = $this->config->From_Email;
    	$fromname = $this->config->From_Name; 
    	$query = "Select distinct email from #__users where id = ".(int)$userid;
    	$db->setQuery($query);
    	$item = $db->loadObjectList();
    	
    	$recipient = $item[0]->email;
    	
    	//    	
    	$query1 = 'select name,subject,content,variable From #__ja_email where upper(name) = '.$db->Quote(upper($emailname));
    	
    	$db->setQuery = ($query1);
    	$itemEmail = $db->loadObjectList();
    	
    	$subject = $itemEmail->subject;
    	$body =  $itemEmail->content;
    	$variable = $itemEmail->variable;
    	
    	$arrVariable = explode(',',$variable);
    	for( $i = 0; $i< count($arrVariable); $i++)
    	{
    		if(count($arrParam) > $i) 
    		{
    			$body = str_replace($arrVariable[$i],$arrParam[$i],$body);
    			$subject = str_replace($arrVariable[$i],$arrParam[$i],$subject);    			
    		}
    	}
    	
    	if ( JUtility::sendMail($from,$fromname,$recipient,$subject,$body,true)  !== true )
		{
			JError::raiseNotice( 500, 'EMAIL_NOT_SENT' );
			return false;
		}	
    	
    	return true;
    }
    /*
		Input: 
		@Username:User send mail
		@EmailName: template name send
		@arrParam: list parametters value 
		OutPut: Success:return 0
			Erro: return Error code
		
	*/
    function sendmail2($userid,$emailname,$listParam)
    {
    	
    	$db = &JFactory::getDBO();
    	
    	$from = $this->config->From_Email;
    	$fromname = $this->config->From_Name;

    	$query = "Select distinct email from #__users where id = ".(int)$userid;
    	$db->setQuery($query);
    	$item = $db->loadObjectList();    	    	    	
    	$recipient = $item[0]->email;    	    	    	
    	$query1 = 'select name,subject,content,variable From #__ja_email where upper(name) = upper('.$db->Quote($emailname).")";
    	    	    	
    	$db->setQuery($query1);
    	$itemEmail = $db->loadObjectList();
    	
    	$subject = $itemEmail[0]->subject;
    	$body =  $itemEmail[0]->content;
    	$variable = $itemEmail[0]->variable;    	    	    	
    	
    	$arrParam = explode(',',$listParam);    	    	
    	
    	$arrVariable = explode(',',$variable);
    	
    	
    	for( $i = 0; $i< count($arrVariable); $i++)
    	{
    		if(count($arrParam) > $i)
    		{
    			$body = str_replace($arrVariable[$i],$arrParam[$i],$body);
    			$subject = str_replace($arrVariable[$i],$arrParam[$i],$subject);
    		}
    	}
    	    	    	
    	if ( JUtility::sendMail($from,$fromname,$recipient,$subject,$body,true)  !== true )
		{
			JError::raiseNotice( 500, 'EMAIL_NOT_SENT' );
			return false;
		}	
    	
    	return true;
    }
    
    
     function sendjobs(){
        
        $MAILTOFRIEND = "MailSendFriend";
        $db = &JFactory::getDBO();
        
        $cid = JRequest::getVar('cid', array(), 'post', 'array');
        JArrayHelper::toInteger( $cid );   
        
        $user = &JFactory::getUser();
        $receipient = JRequest::getVar('friendemail', '', 'post');
        if($user->id > 0 && $receipient){
            $from = $user->email;
            $fromname = $user->name;
            
            $query = 'SELECT id, name, subject, content, variable FROM #__ja_email WHERE UPPER(name) = '.$db->Quote(strtoupper($MAILTOFRIEND));
            $db->setQuery($query);
            $emailtmpl = $db->loadObject();
            
            if($emailtmpl->id > 0){
                $query = "SELECT id, title FROM #__ja_jobs WHERE id IN (".implode(', ', $cid).")";
                $db->setQuery($query);
                $jobs = $db->loadObjectList();
                $arrParams = array();
                $arrParams["[joblink]"] = array();
                for($i = 0; $i < count($jobs); ++$i){
                    $job = $jobs[$i];
                    $arrParams["[joblink]"][] = 
                        "<a href='". JPATH_SITE ."index.php?option=". JBCOMNAME ."view=jajobs&layout=jaview&cid[]=".$job->id."'>"
                        . $job->title ."</a>";
                }
               
                if(count($arrParams["[joblink]"])){
                    $arrParams["[joblink]"] = implode("<br />", $arrParams["[joblink]"]);
                }
                
                // Prepare data
                $arrParams["[username]"] = $user->name;
                $arrParams["[useremail]"] = $user->email;
                $arrParams["[emailsubject]"] = JRequest::getVar('subject', JText::_('Adviced job from Jobboard'), 'post');
                $arrParams["[emailmessage]"] = JRequest::getVar('message', '', 'post');
                
                $body = $emailtmpl->content;
                
                $params = explode(",", $emailtmpl->variable);
                for($i = 0; $i < count($params); ++$i){
                    $body = str_replace(trim($params[$i]),$arrParams[trim($params[$i])],$body);
                }
                
                // Sendmail
                JUtility::sendMail($from, $fromname, $receipient, JText::_('One of your friends sent a job to you'), $body,true);
            }
            
        }
        
    }
}
?>